package com.xyhuo.leetcode.december;

import com.xyhuo.leetcode.common.ListNode;

/**
 * 删除排序链表中的重复元素
 */
public class DeleteDuplicates {

//    public static void main(String[] args) {
//        DeleteDuplicates dd = new DeleteDuplicates();
//        ListNode l1 = new ListNode(1);
//        ListNode l2 = new ListNode(1);
//        ListNode l3 = new ListNode(3);
//        ListNode l4 = new ListNode(3);
//        ListNode l5 = new ListNode(4);
//        ListNode l6 = new ListNode(4);
//        ListNode l7 = new ListNode(5);
//
//        l1.next = l2;
//        l2.next = l3;
//        l3.next = l4;
//        l4.next = l5;
//        l5.next = l6;
//        l6.next = l7;
//
//        dd.deleteDuplicates(l1);
//    }

    public ListNode deleteDuplicates(ListNode head) {
        ListNode left = head, right = head, ans = null;
        while (right != null) {
            ListNode tmp = right;
            while (right.next != null && right.next.val == tmp.val) {
                right = right.next;
            }
            if (right != tmp) {
                left.next = right;
            }
            left = right;
            right = right.next;
            if (ans == null) {
                ans = left;
            }
        }
        return ans;
    }
}
